package dao

import (
	"fmt"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/banner"
	"yundian/internel/app/models/devploys"
	"yundian/internel/app/models/devrule"
	"yundian/internel/app/models/ploys"
	"yundian/internel/app/models/prices"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

type SiteSettingDao struct {
}

func NewSiteSettingDao() *SiteSettingDao {
	return &SiteSettingDao{}
}

//-----计费规则

//计费规则列表
func (d *SiteSettingDao) BillingRulesListDao(baseListReq *request.BaseListReq) *dingo.ErrorWrap {

	var (
		lst    []devrule.DeviceRulesModel
		count  int64
		limit  = baseListReq.PageSize
		offset = baseListReq.PageSize * (baseListReq.PageNo - 1)
	)
	db := global.Orm.Table("device_rule")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

//计费规则编辑
func (d *SiteSettingDao) BillingRulesEditDao(id int, m map[string]interface{}) *dingo.ErrorWrap {

	db := global.Orm.Table("device_rule")
	db.Where("id = ?", id)
	db.Updates(&m)
	if db.Error != nil {
		return dingo.ErrorIf(db.Error, false)
	}

	return dingo.ErrorIf(db.Error, true)
}

//计费规则保存
func (d *SiteSettingDao) BillingRulesSaveDao(param *devrule.DeviceRulesModel) *dingo.ErrorWrap {

	db := global.Orm.Table("device_rule")
	db.Create(&param)
	return dingo.ErrorIf(db.Error, true)
}

//计费规则删除
func (d *SiteSettingDao) BillingRulesDeleteDao(id int) *dingo.ErrorWrap {
	db := global.Orm.Exec("delete from device_rule where id = ?", id)
	return dingo.ErrorIf(db.Error, true)
}

//查询计费规则模板
func (d *SiteSettingDao) GetByIdBillingRulesDao(id int) *dingo.ErrorWrap {
	deviceRulesModel := devrule.DeviceRulesModel{}
	db := global.Orm.Table("device_rule").Where("id = ?", id).Find(&deviceRulesModel)
	return dingo.ErrorIf(db.Error, deviceRulesModel)
}

//应用计费规则
func (d *SiteSettingDao) BillingRulesUseDao(id int, m *prices.PriceModel) *dingo.ErrorWrap {
	db := global.Orm.Debug().Table("prices").Where("id = ?", id).Updates(m)
	return dingo.ErrorIf(db.Error, true)
}

//-----奖励规则

//奖励规则列表
func (d *SiteSettingDao) RewardPloyListDao(baseListReq *request.BaseListReq) *dingo.ErrorWrap {

	var (
		lst    []devploys.DevicePloysModel
		count  int64
		limit  = baseListReq.PageSize
		offset = baseListReq.PageSize * (baseListReq.PageNo - 1)
	)
	db := global.Orm.Table("device_ploys")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

//奖励规则编辑
func (d *SiteSettingDao) RewardPloyEditDao(id int, m map[string]interface{}) *dingo.ErrorWrap {
	db := global.Orm.Table("device_ploys")
	db.Where("id = ?", id)
	db.Updates(&m)
	if db.Error != nil {
		return dingo.ErrorIf(db.Error, false)
	}

	return dingo.ErrorIf(db.Error, true)
}

//奖励规则保存
func (d *SiteSettingDao) RewardPloySaveDao(param *devploys.DevicePloysModel) *dingo.ErrorWrap {

	db := global.Orm.Table("device_ploys")
	db.Create(&param)
	return dingo.ErrorIf(db.Error, true)
}

//奖励规则删除
func (d *SiteSettingDao) RewardPloyDeleteDao(id int) *dingo.ErrorWrap {
	db := global.Orm.Exec("delete from device_ploys where id = ?", id)
	return dingo.ErrorIf(db.Error, true)
}

//查询奖励规则模板
func (d *SiteSettingDao) GetByIdRewardPloyDao(id int) *dingo.ErrorWrap {
	devicePloysModel := devploys.DevicePloysModel{}
	db := global.Orm.Table("device_ploys").Where("id = ?", id).Find(&devicePloysModel)
	return dingo.ErrorIf(db.Error, devicePloysModel)
}

//应用奖励规则
func (d *SiteSettingDao) RewardPloyUseDao(ploysId int, m *ploys.PloyModel) *dingo.ErrorWrap {
	db := global.Orm.Debug().Table("ploys").Where("id = ? ", ploysId).Updates(m)
	return dingo.ErrorIf(db.Error, true)
}

//------广告配置

//广告列表
func (d *SiteSettingDao) GetBannerList(param *request.BannerListReq) *dingo.ErrorWrap {
	var (
		lst    []*banner.BannerModel
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)
	db := global.Orm.Debug().Table("banner")

	if param.KaName != "" {
		db.Where("ka_name like ?", fmt.Sprint("%", param.KaName, "%"))
	}

	if param.BannerName != "" {
		db.Where("banner_name like ?", fmt.Sprint("%", param.BannerName, "%"))
	}

	db.Order("create_at desc")

	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)

	return dingo.ErrorIf(db.Error, lst, count)
}

//广告修改
func (d *SiteSettingDao) BannerUpdate(bannerModel *banner.BannerModel) *dingo.ErrorWrap {
	db := global.Orm.Table("banner").Where("id = ?", bannerModel.Id).Model(&bannerModel).Updates(bannerModel)
	return dingo.ErrorIf(db.Error, true)
}

//广告新增
func (d *SiteSettingDao) BannerInsert(bannerModel *banner.BannerModel) *dingo.ErrorWrap {
	db := global.Orm.Table("banner").Model(&bannerModel).Create(&bannerModel)
	return dingo.ErrorIf(db.Error, true)
}

func (d *SiteSettingDao) BannerDelete(id int) *dingo.ErrorWrap {
	db := global.Orm.Exec("delete from banner where id = ?", id)
	return dingo.ErrorIf(db.Error, true)
}
